<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" v-show="showSearch" label-width="68px">
      <el-form-item label="聊天会话id" prop="conversationId">
        <el-input
          v-model="queryParams.conversationId"
          placeholder="请输入聊天会话id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送人id" prop="sendUserId">
        <el-input
          v-model="queryParams.sendUserId"
          placeholder="请输入发送人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接收人id" prop="receiveUserId">
        <el-input
          v-model="queryParams.receiveUserId"
          placeholder="请输入接收人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:messageLog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:messageLog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:messageLog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:messageLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messageLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" prop="" width="50" align="center">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)"> </span>
        </template>
      </el-table-column>
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="聊天会话id" align="center" prop="conversationId" />

      <el-table-column label="消息类型【1=文字 2=图片 3=文件】" align="center" prop="type" />

      <el-table-column label="内容" align="center" prop="content" />

      <el-table-column label="发送人id" align="center" prop="sendUserId" />

      <el-table-column label="接收人id" align="center" prop="receiveUserId" />

      <el-table-column label="消息状态【0=未读 1=已读】" align="center" prop="readStatus" />

      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column label="操作" align="center"  class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              v-if="scope.row.status==='inactive'"
              size="mini"
              type="text"
              icon="el-icon-check"
              style="color: #4AB7BD"
              @click="handleChangeStatus(scope.row)"
              v-hasPermi="['biz:messageLog:changeStatus']"
          >启用
          </el-button>
          <el-button
              v-if="scope.row.status==='active'"
              size="mini"
              type="text"
              icon="el-icon-close"
              style="color: #E65D6E"
              @click="handleChangeStatus(scope.row)"
              v-hasPermi="['biz:messageLog:changeStatus']"
          >禁用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:messageLog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:messageLog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改聊天消息记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="聊天会话id" prop="conversationId">
          <el-input v-model="form.conversationId" maxlength="20" placeholder="请输入聊天会话id" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="发送人id" prop="sendUserId">
          <el-input v-model="form.sendUserId" maxlength="20" placeholder="请输入发送人id" />
        </el-form-item>
        <el-form-item label="接收人id" prop="receiveUserId">
          <el-input v-model="form.receiveUserId" maxlength="20" placeholder="请输入接收人id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitLoading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMessageLog, getMessageLog, delMessageLog, addMessageLog, updateMessageLog, changeStatus } from "@/api/biz/messageLog";

export default {
  name: "MessageLog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 聊天消息记录表格数据
      messageLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        conversationId: undefined,
        type: undefined,
        content: undefined,
        sendUserId: undefined,
        receiveUserId: undefined,
        readStatus: undefined,
      },
      // 表单参数
      form: {},
      submitLoading: false,
      // 表单校验
      rules: {
        conversationId: [
          { required: true, message: "聊天会话id不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "消息类型【1=文字 2=图片 3=文件】不能为空", trigger: "change" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        sendUserId: [
          { required: true, message: "发送人id不能为空", trigger: "blur" }
        ],
        receiveUserId: [
          { required: true, message: "接收人id不能为空", trigger: "blur" }
        ],
        readStatus: [
          { required: true, message: "消息状态【0=未读 1=已读】不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询聊天消息记录列表 */
    getList() {
      this.loading = true;
      listMessageLog(this.queryParams).then(response => {
        this.messageLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getIndex($index) {
      //表格序号
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + $index + 1
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        conversationId: null,
        type: null,
        content: null,
        sendUserId: null,
        receiveUserId: null,
        readStatus: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加聊天消息记录";
    },
    handleChangeStatus(row) {
      let statusText = row.status === 'active' ? '禁用' : '启用'
      let status = row.status === 'active' ? 'inactive' : 'active'
      const ids = [row.id] || this.ids;
      this.$confirm('是否确认' + statusText + '该项数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return changeStatus({ids: ids, status: status});
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      }).catch(function () {
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMessageLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改聊天消息记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id != null) {
            updateMessageLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.submitLoading = false;
              this.getList();
            }).catch(() => {
              this.submitLoading = false;
            });
          } else {
            addMessageLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.submitLoading = false;
              this.getList();
            }).catch(() => {
              this.submitLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除聊天消息记录编号为"' + ids + '"的数据项？').then(function() {
        return delMessageLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/messageLog/export', {
        ...this.queryParams
      }, `messageLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
